local ClientOHOH = _G.ClientOHOH;

local M = ClientOHOH.class.new(script, ClientOHOH.files.OHOHOLYBUIBase());
local CallBackManager = ClientOHOH.load.CallBackManager();
local BtnHelper = ClientOHOH.load.BtnHelper();
local LanguageHelper=_G.LanguageHelper 
local BigNumberHelper = ClientOHOH.load.BigNumberHelper();
local ConnectClass=_G.CommonOHOH.load.Connect();

function InitInsts(ui)
	ui.insts = {};

	ui.insts.close ="ctrl.close"
	ui.insts.content ={}

	for i=1,6 do
		ui.insts.content[i]={
			Count="ctrl."..i..".num",
			Btn="ctrl."..i..".claimButton",
			Day="ctrl."..i..".text",
		}
	end
	table.insert(ui.insts.content,{
		buff_count="ctrl.7.num1",
		base_meat="ctrl.7.num2",
		base_diamond="ctrl.7.num3",

		Btn="ctrl.7.claimButton",
		Day="ctrl.7.text",
	})
	
end



function M.new(...)
	return M(...);
end;

function M.ctor(self)
	InitInsts(self)
	self.ConList={}
	M.super.ctor(self, ClientOHOH:GetPath_Loop("ReplicatedStorage.Resource.UI.DaySinGui"));

    self.ClaimAction=ConnectClass.new()

	CallBackManager:AddRbxConnect(self,self.insts.close.Activated, function()
        self:Hide()
	end);

end;


function M.dtor(self)
	M.super.dtor(self);
end;

-- 根据奖励计算数量
function M.GetCount(self,award)
	for k,v in pairs(award) do
		return v
	end

	return 0
end
--获取药水数量
function M.GetBuffCount(self,award)
	local count=0
	for k,v in pairs(award) do
		local list=string.split(k,"_")
		if list[1]=="buff" then
			count=count+1
		end
	end
	return count
end

function M.FullCount(self,cell,v)
	
	local award=v.award
	if v.id==7 then
		cell.buff_count.Text="x"..self:GetBuffCount(award).." "..LanguageHelper:GetText(20605) 
		cell.base_meat.Text="x"..BigNumberHelper:ToString(award.base_meat).." "..LanguageHelper:GetText(3315013) 
		cell.base_diamond.Text="x"..BigNumberHelper:ToString(award.base_diamond).." "..LanguageHelper:GetText(20600) 
	else
		local count=self:GetCount(award)
		if count>1 then
			cell.Count.Text="X"..BigNumberHelper:ToString(count)
			cell.Count.Visible=true
		else
			cell.Count.Visible=false
		end
	end

end

-- 参数：奖励数据，签到Id，不能签到Id，旧数据Id
function M.Init(self,data,id,canNotSigin,oldSinId)
	for _,v in pairs(self.ConList) do
		v:Disconnect()
	end
	self.ConList={}
    for k,v in pairs(data) do
		local cell= self.insts.content[k]
			cell.Day.Visible=true
			cell.Day.Text=LanguageHelper:GetText(30037,{"%%ARG1"},{k}) 
			cell.Btn.Visible=false
			cell.Btn.text.Text=LanguageHelper:GetText(10403) 
			-- cell.Name.Text=LanguageHelper:GetText(v.name) 
		if k<id  then --已经领取
			cell.Day.Text=LanguageHelper:GetText(10404) 
		elseif k==id then --下一个领取
			if canNotSigin then
				-- cell.Day.Text=LanguageHelper:GetText(10404) 
			else --可以领取
				-- cell.Day.Text=LanguageHelper:GetText(10403) 
				cell.Btn.Visible=true
				cell.Day.Visible=false
				self.ClickConnect=CallBackManager:AddRbxConnect(self, cell.Btn.Activated, function()
					local arg=self.ClaimAction:Action()
					for k,v in pairs(arg) do
						if v then
							cell.Btn.Visible=false
							cell.Day.Text=LanguageHelper:GetText(10404) 
							cell.Day.Visible=true
							if self.ClickConnect then
								self.ClickConnect:Disconnect()
								self.ClickConnect=nil
							end
							return
						end
					end
				end);
			end

		else -- 等待领取
			-- cell.State.Text=LanguageHelper:GetText(30038) 
			-- cell.Button.ImageColor3=willC
		end
		self:FullCount(cell,v)

		local con=v.DataChange:Connect(function()
			self:FullCount(cell,v)
		end)
		table.insert(self.ConList,con)
	
    end
end

return M
